/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfonia.images.ImageConstants;
import org.jfonia.images.ScaledImageIcon;
import org.jfonia.language.DescriptionConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;

/**
 *
 * @author Rik Bauwens
 */
public class LoaderPanel extends JPanel implements ComponentListener
{
        private Component parent;
        private JLabel descriptionLabel;
        private JLabel loaderLabel;

        public LoaderPanel(Component parent)
        {
                super();
                setParent(parent);
                setVisible(false);
                //setOpaque(false);
                setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
                add(Box.createHorizontalGlue());

                JPanel contentPanel = new JPanel();
                contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
                contentPanel.add(Box.createVerticalGlue());

                descriptionLabel = new JLabel(LanguageResource.getInstance()
                                              .getLabel(LabelConstants.LOADING_LEADSHEET));
                Box descriptionBox = Box.createHorizontalBox();
                descriptionBox.add(Box.createHorizontalGlue());
                descriptionBox.add(descriptionLabel);
                descriptionBox.add(Box.createHorizontalGlue());
                contentPanel.add(descriptionBox);

                ImageIcon icon = new ScaledImageIcon(ImageConstants.LOADER, 
                        LanguageResource.getInstance().getDescription
                        (DescriptionConstants.LOADING)).getImageIcon();
                loaderLabel = new JLabel(icon);
                Box loaderBox = Box.createHorizontalBox();
                loaderBox.add(Box.createHorizontalGlue());
                loaderBox.add(loaderLabel);
                loaderBox.add(Box.createHorizontalGlue());
                contentPanel.add(loaderBox);

                contentPanel.add(Box.createVerticalGlue());
                add(contentPanel);
                add(Box.createHorizontalGlue());
        }

        private void setParent(Component parent)
        {
                if(this.parent != null)
                        this.parent.removeComponentListener(this);
                this.parent = parent;
                setBounds(0, 0, parent.getWidth(), parent.getHeight());
                parent.addComponentListener(this);
        }

        public LoaderPanel setDescription(String description)
        {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(LanguageResource.getInstance()
                                     .getLabel(LabelConstants.LOADING_LEADSHEET));
                if(description != null)
                {
                        stringBuilder.append(" ");
                        stringBuilder.append(description);
                }
                descriptionLabel.setText(stringBuilder.toString());
                return this;
        }

        /*@Override
        protected void paintComponent(Graphics g)
        {
                Graphics2D g2 = (Graphics2D) g;
                //When the component is dragged, the alpha channel is adjusted to
                //highlight the textfield
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.5));
                g2.fillRect(0, 0, getWidth(), getHeight());
                super.paintComponent(g);
        }*/

        public void componentResized(ComponentEvent e)
        {
                setBounds(0, 0, parent.getWidth(), parent.getHeight());
        }

        public void componentMoved(ComponentEvent e) {}

        public void componentShown(ComponentEvent e) {}

        public void componentHidden(ComponentEvent e) {}
}
